package com.fenghongzhang.day016;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<Food.DataBean> list ;
    private Context context;

    public MyAdapter(List<Food.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    //一共有 多少个布局
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    /**
     *每一行应该对应哪个布局
     * @param position
     * @return 返回一个布局的表示
     */
    @Override
    public int getItemViewType(int position) {
        if(position % 2 == 0){
            return 0;
        }
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        VHRight vhRight;
        if(convertView == null){
            if(getItemViewType(position) == 0){
                convertView = LayoutInflater.from(context).inflate(R.layout.lv_layout,null);
                vh = new VH();
                vh.lv_img = convertView.findViewById(R.id.lv_img);
                vh.lv_text = convertView.findViewById(R.id.lv_text);
                convertView.setTag(vh);
            }else{
                convertView = LayoutInflater.from(context).inflate(R.layout.right_layout,null);
                vhRight = new VHRight();
                vhRight.lv_img = convertView.findViewById(R.id.right_img);
                vhRight.lv_text = convertView.findViewById(R.id.right_text);
                convertView.setTag(vhRight);
            }
        }else{
            if(getItemViewType(position) == 0){
                vh = (VH) convertView.getTag();
            }else{
                vhRight = (VHRight) convertView.getTag();
            }
        }
        if(getItemViewType(position) == 0){
            vh = (VH) convertView.getTag();
            vh.lv_text.setText(list.get(position).getTitle());
        }else{
            vhRight = (VHRight) convertView.getTag();
            vhRight.lv_text.setText(list.get(position).getTitle());
        }
       return convertView;
    }
    //vh里面是用来缓存findId地址值的,所以,布局文件中,有了那个组件,这个就要加什么组件
    class VH{
        TextView lv_text;
        ImageView lv_img;
    }

    class VHRight{
        TextView lv_text;
        ImageView lv_img;
    }

}
